//
//  NSWLMHeaderView.m
//  Meboo
//
//  Created by kyo on 2024/4/30.
//

#import "NSWLMHeaderView.h"
#import "NSMessageView.h"
#import "NSWLMInfoPopView.h"
@interface NSWLMHeaderView()
@property(nonatomic,strong) UILabel *titleLab;
@property(nonatomic,strong) UILabel *subTitleLab;
@property(nonatomic,strong) UIButton *infoBtn;
@property(nonatomic,strong) NSMessageView *chatBtn;
@property(nonatomic,strong) UIButton *PremiumBtn;

@property(nonatomic,strong) NSWLMInfoPopView *infoPopView;
@end

@implementation NSWLMHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self resetState];
    }
    return self;
}

#pragma mark - Public Metohd
- (void)resetState {
    if ([MBUserAccount defaultAccount].isVip) {
        self.subTitleLab.text = [NSString stringWithFormat:@"%ld people have already liked you 💗",[MBWhoLikeMeDataManager sharedManager].listDataArr.count];
        if ([MBWhoLikeMeDataManager sharedManager].listDataArr.count == 0) {
            self.subTitleLab.hidden = YES;
            [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(MBHeightScale(-5));
            }];
        } else {
            self.subTitleLab.hidden = NO;
            [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(MBHeightScale(-25));
            }];
        }
        self.PremiumBtn.hidden = YES;
    } else {
        self.subTitleLab.hidden = NO;
        self.subTitleLab.text = @"Go Premium for instant matches and unlocked features!";
        self.PremiumBtn.hidden = NO;
        [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(MBHeightScale(-40));
        }];
    }
}
#pragma mark - Action
- (void)infoAction {
    self.infoPopView.hidden = NO;
    [_infoBtn setImage:ImageWithName(@"wlm_header_info_select") forState:0];
}

- (void)premiumAction {
    if (self.vipBlock) {
        self.vipBlock();
    }
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.chatBtn];
    [self.chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(StatusBarHeight+MBHeightScale(6));
        make.right.mas_equalTo(MBWidthScale(-20));
        make.width.height.mas_equalTo(MBHeightScale(36));
    }];
    
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.chatBtn).offset(MBHeightScale(4));
        make.left.mas_equalTo(MBWidthScale(24));
        make.bottom.mas_equalTo(MBHeightScale(-40));
    }];
    
    [self addSubview:self.infoBtn];
    [self.infoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLab).offset(MBHeightScale(3));
//        make.centerX.equalTo(self.mas_left).offset((ScreenWidth-MBWidthScale(74))*3/5+MBWidthScale(37));
        make.left.equalTo(self.titleLab.mas_right).offset(MBWidthScale(3));
        make.width.height.mas_equalTo(MBHeightScale(17));
    }];
    
    [self addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(MBHeightScale(10));
        make.right.equalTo(self.mas_centerX).offset(MBWidthScale(40));
        make.left.mas_equalTo(MBWidthScale(24));
    }];
    
    [self addSubview:self.PremiumBtn];
    [self.PremiumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(MBHeightScale(26));
        make.width.mas_equalTo(MBWidthScale(90));
        make.right.mas_equalTo(MBWidthScale(-15));
        make.centerY.equalTo(self.subTitleLab);
    }];
    
    [kKeyWindow addSubview:self.infoPopView];
}

#pragma mark - Setter && Getter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.text = @"Who Like Me";
        _titleLab.font = PDBoldScale(30);
        _titleLab.textColor = HEXS(@"B92176");
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel new];
        _subTitleLab.numberOfLines = 0;
        _subTitleLab.text = @"Go Premium for instant matches and unlocked features!";
        _subTitleLab.font = PDRegularScale(12);
        _subTitleLab.textColor = HEXS(@"666666");
    }
    return _subTitleLab;
}
- (UIButton *)infoBtn {
    if (!_infoBtn) {
        _infoBtn = [UIButton new];
        [_infoBtn setYXEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
        [_infoBtn setImage:ImageWithName(@"wlm_header_info_normal") forState:0];
        [_infoBtn addTarget:self action:@selector(infoAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _infoBtn;
}

- (UIButton *)PremiumBtn {
    if (!_PremiumBtn) {
        _PremiumBtn = [UIButton leftimageAndRighttitlePatternWithImage:@"wlm_header_vip_icon" AndMargin:5 AndTitle:@"Premium" AndTitleFont:MBHelFontSizeScale(12) AndTitleColor:HEXS(@"ffffff")];
        [_PremiumBtn setBackgroundColor:HEXS(@"B92176")];
        _PremiumBtn.layer.cornerRadius = MBHeightScale(26/2);
        [_PremiumBtn addTarget:self action:@selector(premiumAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _PremiumBtn;
}

- (NSMessageView *)chatBtn {
    if (!_chatBtn) {
        _chatBtn = [NSMessageView new];
    }
    return _chatBtn;
}

- (NSWLMInfoPopView *)infoPopView {
    if (!_infoPopView) {
        __WeakSelf
        _infoPopView = [[NSWLMInfoPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _infoPopView.closeBlock = ^{
            [weakSelf.infoBtn setImage:ImageWithName(@"wlm_header_info_normal") forState:0];
        };
    }
    return _infoPopView;
}
@end
